<?php
if (!file_exists('../install.lock')) {
    header('Location: ../install.php');
    exit;
}

require_once 'config.php';

$settings = getSettings();
$order = null;

$order_no = $_GET['order_no'] ?? '';

if (empty($order_no)) {
    header('Location: index.php');
    exit;
}

// 获取订单信息
$stmt = $pdo->prepare("SELECT o.*, p.name as product_name, p.description as product_description, p.duration_type, p.duration_value FROM orders o LEFT JOIN products p ON o.product_id = p.id WHERE o.order_no = ?");
$stmt->execute([$order_no]);
$order = $stmt->fetch();

if (!$order) {
    header('Location: index.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <title><?php echo htmlspecialchars($settings['title'] ?? '卡密购买'); ?> - 支付失败</title>
    <link rel="shortcut icon" type="image/x-icon" href="../assets/favicon.ico">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-touch-fullscreen" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <link rel="stylesheet" type="text/css" href="../assets/css/materialdesignicons.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/animate.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/style.min.css">
    <style>
        .failed-container {
            max-width: 600px;
            margin: 0 auto;
        }
        .failed-icon {
            font-size: 80px;
            color: #dc3545;
        }
        .order-info {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        @media (max-width: 768px) {
            .mobile-bottom-bar {
                position: fixed;
                left: 0;
                right: 0;
                bottom: 0;
                z-index: 999;
                background: #fff;
                box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
                padding: 10px 0 10px 0;
            }
        }
        main.lyear-layout-content {
            padding-top: 0 !important;
            margin-top: 0 !important;
            background: none !important;
            box-shadow: none !important;
        }
    </style>
</head>

<body>
<!--页面loading-->
<div id="lyear-preloader" class="loading">
    <div class="ctn-preloader">
        <div class="round_spinner">
            <div class="spinner"></div>
            <img src="../assets/images/loading-logo.png" alt="">
        </div>
    </div>
</div>
<!--页面loading end-->

<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!-- 导航栏 -->
        <nav class="navbar navbar-expand-lg navbar-light bg-white border-bottom w-100" style="margin:0;padding:0;">
            <div class="container-fluid px-4">
                <a class="navbar-brand" href="index.php">
                    <?php echo htmlspecialchars($settings['title'] ?? '卡密购买'); ?>
                </a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarNav">
                    <ul class="navbar-nav ms-auto">
                        <li class="nav-item">
                            <a class="nav-link" href="../index.php">返回首页</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="orders.php">我的订单</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
        
        <!--页面主要内容-->
        <main class="lyear-layout-content">
            <div class="container p-t-5" style="margin-top:10px;min-height:100vh;display:flex;flex-direction:column;justify-content:flex-start;padding-bottom:70px;">
                <div class="row justify-content-center mx-0">
                    <div class="col-lg-6 col-md-8 mx-auto">
                        <!-- 失败提示卡片 -->
                        <div class="card mb-4">
                            <div class="card-body text-center">
                                <div class="failed-icon mb-3">
                                    <i class="mdi mdi-close-circle"></i>
                                </div>
                                <h2 class="text-danger">支付失败</h2>
                                <p class="text-muted">
                                    <?php if ($order['status'] === 'expired'): ?>
                                        订单已超时，请重新购买
                                    <?php else: ?>
                                        支付未完成，请重新尝试
                                    <?php endif; ?>
                                </p>
                            </div>
                        </div>
                        <!-- 订单信息卡片 -->
                        <div class="card mb-4">
                            <div class="card-body">
                                <h5 class="mb-3"><i class="mdi mdi-receipt"></i> 订单信息</h5>
                                <div class="row">
                                    <div class="col-md-6">
                                        <p><strong>订单号：</strong><?php echo htmlspecialchars($order['order_no']); ?></p>
                                        <p class="mb-1"><strong>商品名称：</strong><?php
                                $type_map = [
                                    'minute' => '分钟',
                                    'hour' => '小时',
                                    'day' => '天',
                                    'week' => '周',
                                    'month' => '月',
                                    'season' => '季',
                                    'quarter' => '季',
                                    'year' => '年',
                                    'forever' => '永久',
                                    '分钟' => '分钟',
                                    '小时' => '小时',
                                    '天' => '天',
                                    '周' => '周',
                                    '月' => '月',
                                    '季' => '季',
                                    '年' => '年',
                                    '永久' => '永久'
                                ];
                                $type = $type_map[$order['duration_type']] ?? $order['duration_type'];
                                if ($order['duration_type'] === 'forever' || $order['duration_type'] === '永久') {
                                    echo htmlspecialchars($order['product_name']) . '';
                                } else if (!empty($order['duration_value']) && !empty($type)) {
                                    echo htmlspecialchars($order['product_name']) . '(' . $order['duration_value'] . $type . ')';
                                } else {
                                    echo htmlspecialchars($order['product_name']);
                                }
                                ?></p>
                                    </div>
                                    <div class="col-md-6">
                                        <p><strong>购买数量：</strong><?php echo $order['quantity']; ?> 张</p>
                                        <p><strong>订单金额：</strong><span class="text-danger fw-bold">￥<?php echo number_format($order['total_amount'], 2); ?></span></p>
                                    </div>
                                </div>
                                <p><strong>创建时间：</strong><?php echo date('Y-m-d H:i:s', strtotime($order['created_at'])); ?></p>
                                <p><strong>订单状态：</strong>
                                    <?php if ($order['status'] === 'expired'): ?>
                                        <span class="badge bg-warning">已过期</span>
                                    <?php else: ?>
                                        <span class="badge bg-secondary">未支付</span>
                                    <?php endif; ?>
                                </p>
                            </div>
                        </div>
                        
                        <!-- 操作按钮 -->
                        <div class="text-center mb-4">
                            <?php if ($order['status'] !== 'expired'): ?>
                                <a href="payment.php?order_no=<?php echo $order_no; ?>" class="btn btn-primary btn-sm me-2 mb-2">
                                    <i class="mdi mdi-credit-card"></i> 重新支付
                                </a>
                            <?php endif; ?>
                            <a href="index.php" class="btn btn-danger btn-sm me-2 mb-2">
                                <i class="mdi mdi-cart-plus"></i> 重新购买
                            </a>
                            <a href="orders.php" class="btn btn-primary btn-sm me-2 mb-2">
                                <i class="mdi mdi-format-list-bulleted"></i> 查看订单
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</div>

<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/popper.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
<script src="../assets/js/perfect-scrollbar.min.js"></script>
<script src="../assets/js/main.min.js"></script>
</body>
</html> 